﻿<?php
include 'PublicdetailController.php';
class TableController extends PublicdetailController {
    public $idnguoidung;
    public $nguoidung;
    public function init() {
		$this->initValue ();
        $this->idnguoidung = $this->TblUserbyId[0]['id'];
        $this->nguoidung = $this->TblUserbyId[0]['name'];
	}
	public function indexAction() {
	if($this->TblUserbyId[0]['roles']!='Admin' && $this->TblUserbyId[0]['roles']!='Quanly')
		{
		echo "<script type=\"text/javascript\">
			    alert('Bạn không có quyền vào mục này!');
				 window.history.back();
			</script>";
		die;
		}
		else
        $this->view->iduser = $this->idnguoidung;
		$this->view->table = @$_REQUEST['table'];
        $this->_loadSetting(@$_REQUEST['table']);                
        }
	public function addAction() {
		$this->_helper->layout ()->disableLayout ();
		$table = $this->view->table = $_REQUEST['table'];
		$data = $_REQUEST['data'];
        if(@$_REQUEST['table'] =='user')
        {
		if($data['roles']==1){$data['roles']='Quanly';}
		else {$data['roles']='Tieptan';}
         }		
          $data['createdDate'] = date('Y-m-d H:i:s'); 
		if (file_exists('tables/editing/' . $table . '.php')) {
			require 'tables/editing/' . $table . '.php';
		}
		$tableModel = new Model_Table();
		$tableModel->addObj($_REQUEST['table'], $data);
		$jsonObj = array();
		$jsonObj ["success"] = true;
		
		echo json_encode ( $jsonObj );
		die();
	}
	
	public function editAction() {
		$this->_helper->layout ()->disableLayout ();
		
		$table = $this->view->table = $_REQUEST['table'];
		$data = $_REQUEST['data'];
		if(@$_REQUEST['table'] =='user')
        {
		if($_REQUEST['id'] ==1 && $this->TblUserbyId[0]['roles']=='Quanly'){
		
		echo "<script type=\"text/javascript\">
			    alert('Bạn không có quyền vào mục này!');
				 window.history.back();
			</script>";
		die;
		}
		else
		if($data['roles']==1){$data['roles']='Quanly';}
		else {$data['roles']='Tieptan';}
         }
		 $data['createdDate'] = date('Y-m-d H:i:s'); 
         // $data['content'] = $_REQUEST['content'];
		if (file_exists('tables/updating/' . $table . '.php')) {
			require 'tables/updating/' . $table . '.php';
		}
		$tableModel = new Model_Table();
		$tableModel->updateObj($_REQUEST['table'], $data, $_REQUEST['id']);
		$jsonObj = array();
		$jsonObj ["success"] = true;
		
		echo json_encode ( $jsonObj );
		die();
	}
	
	public function delAction() {
	if($this->TblUserbyId[0]['roles']!='Admin')
		{
		echo "<script type=\"text/javascript\">
			    alert('Bạn không có quyền vào mục này!');
				 window.history.back();
			</script>";
		die;
		}
		$this->_helper->layout ()->disableLayout ();
		$id = $_REQUEST ['id'];
		$table = $this->view->table = $_REQUEST['table'];
		$tableModel = new Model_Table();
		// $id = $this->_getParam ( 'id' );
		$_Id = null;
		$jsonObjv = null;	
		if (isset ( $id ) && $id > 0) {
			$_Id = $tableModel->del ( $table,$id);
		} else {
			return;
		}
		
		if (! $_Id) {
			$jsonObj = json_encode ( array (
					'success' => false 
			) );
		} else {
			$jsonObj = json_encode ( array (
					'success' => true 
			) );
		}
		die;
	}
	
	public function jsonAction() {
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 1;
		$sort = isset ( $_POST ['sort'] ) ? strval ( $_POST ['sort'] ) : 'id';
		$order = isset ( $_POST ['order'] ) ? strval ( $_POST ['order'] ) : 'asc';
		$offset = ($page - 1) * $rows;
		$this->_helper->layout ()->disableLayout ();
		$table = $this->view->table = $_REQUEST['table'];
		$settings = $this->_loadSetting($_REQUEST['table']);
		$options = array(
			'page' => $page,
			'rows' => $rows,
			'sort' => $sort,
			'order' => $order,
			'offset' => $offset
		);
		$tableModel = new Model_Table ();
        if($table=='catalog'){
        $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) :1;
		$jsonObj = json_encode($tableModel->getItems($id,$table,$settings));
        }
         else{
            $jsonObj = json_encode ( $tableModel->getFetObj ( $table, $settings, $options ) );
                       
           }                       
  
		//$jsonObj = json_encode ( $tableModel->getFetObj ( $table, $settings, $options,$iscat ) );		
		return $this->view->jsonObj = $jsonObj;
	}
	
	public function comboroleAction()
	{
		$this->_helper->layout()->disableLayout();
		$comborole = new Model_Table ();
		$jsonObj = json_encode($comborole->getComborole());
		return $this->view->jsonObj = $jsonObj;
	}
    public function comboparenttreeAction()
	{
		$this->_helper->layout()->disableLayout();
		$comborole = new Model_Table ();
		$jsonObj = json_encode($comborole->getParenttree());
		return $this->view->jsonObj = $jsonObj;
	}
    public function comboiscatAction()
	{      
		$this->_helper->layout()->disableLayout();
            $arrcombo = array(array("id"=>0,"text"=>"no"),
            array("id"=>1,"text"=>"yes")
            );                         
		$jsonObj = json_encode($arrcombo);         
		return $this->view->jsonObj = $jsonObj;
	}
    public function comboroomAction()
	{      
		$this->_helper->layout()->disableLayout();
            $arrcombo = array(array("id"=>"Room","text"=>"Room"),
            array("id"=>"Service","text"=>"Service"),array("id"=>"Billard","text"=>"Billard")
            );                         
		$jsonObj = json_encode($arrcombo);         
		return $this->view->jsonObj = $jsonObj;
	}                  
	
	public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != 'login') {
				$this->_redirect ( '/index/login' );
			}
		}
	}
	
    public function _loadSetting($table) {
		require 'tables/'. $table.'.php';
	}
}